﻿using System;
using System.Configuration;
using System.IO;
using Citi.Helpers;
using Citi.RoutineBase;
using System.Collections.Generic;

namespace Citi.GenericApplications.RefreshExcelFile.ConsoleUi
{
    class CurrentAction : ICurrentAction
    {
        public string Description
        {
            set { Console.WriteLine(value); }
        }

        public void StartLog(string message)
        {

        }

        public void StartLogOnFile(string filePath)
        {

        }

        public void StopLog()
        {

        }

        public void StopLogOnFile()
        {

        }
    }

    class Program
    {
        static string LogPath;
        static string TempPath = @"C:\Temp\{0}";

        static void Main(string[] args)
        {
            ICurrentAction currentAction = new CurrentAction();
            //Refresh Excel
            //var parameters = new Dictionary<string, object>
            //{ { "ExcelFile", @"C:\temp\coa\1c14d94d-f9fc-4f6e-a5e7-55628e2936ff.xls" }
            //    , { "PwdFile", "" }
            //, {"NewSheetName","Report Detailed"}
            //, {"PathToSaveRefreshedFile", @"C:\Users\nd61955\Downloads\PTSBookingReport.xlsx"}
            //};

            //Join sheets Excel
            var parameters = new Dictionary<string, object>
            { { "ExcelFile", @"C:\Users\nd61955\Desktop\Excel\PTS Time Booking - 2 - LATAM Excluding Brazil - Sep.xls" }
                , { "PwdFile", "" }
            , {"NewSheetName","Report Detailed"}
            //, {"PathToSaveJoinedFile", @"\\gruvnascti0016.wlb.lac.nsroot.net\DATA_GRP1\FTOOLS\Inputs\FinancePlanningAnalysis\Pearl\tb_pearl.xlsx"}
            , {"PathToSaveJoinedFile", @"C:\temp\PTS Time Booking - 2 - LATAM Excluding Brazil - Sep.xlsx"}
            };


            //Email report finished
            //var parameters = new Dictionary<string, object>
            //    {
            //        {"RobotUsername", @"lac\lac_bra_ftools"},
            //        {"RobotPassword", "Ferrari3k10"},
            //        {"ReportFolder", @"\\gruvnascti0016.wlb.lac.nsroot.net\DATA_GRP1\FTOOLS\Apps\GOCAttributes"},
            //        {"ReportExtension", ".xlsx"},
            //        {"ReportPattern", @"GOCAttributes_201409"},
            //        {"FriendlyReportName", @"PTS LAG Report - Current Year"},
            //        {"EmailAddressUsers", "nd61955"},
            //        {"EmailAddressError", "nd61955"}
            //    };

            //Upload sharepoint
            //var parameters = new Dictionary<string, object>
            //{ { "SharepointUrl", @"https://globalconsumer.collaborationtools.consumer.citigroup.net/sites/OTFPC/ICGOnTFinMan" }
            //    , { "ListName", "PTS Reports" }
            //, {"UploadPath",@"\\gruvnascti0009.wlb.lac.nsroot.net\DATA_GRP1\FPCRelat\PTS Report\BookOfWorkLAG2014_20140704.xlsb"}
            //, {"SPFolderToUpload", "Brazil ICG Tech and GFTS BoW"}
            //, {"OverwriteExistingFile", true}};

            //Copy files
            //var parameters = new Dictionary<string, object>
            //{ 
            //    { "FilesToCopy", @"C:\temp\teste.xls" }
            //    , { "Destination", @"C:\test" }
            //};


            //new SchedulerUi.CopyFiles().Start(DateTime.Today, ref currentAction, parameters);
            //new SchedulerUi.EmailReportFinished().Start(DateTime.Today, ref currentAction, parameters);
            new SchedulerUi.JoinSheets().Start(DateTime.Today, ref currentAction, parameters);
            //new SchedulerUi.UploadSharepoint().Start(DateTime.Today, ref currentAction, parameters);

            //return;
            //if (string.IsNullOrWhiteSpace(pathToSaveRefreshedFile))
            //    pathToSaveRefreshedFile = excelFile;

            //LogPath = string.Format(ConfigurationManager.AppSettings["LogPath"], Path.GetFileNameWithoutExtension(excelFile)); 

            //if (File.Exists(LogPath))
            //    File.Delete(LogPath);

            //var writer = new ConsoleInFile(LogPath);
            //Console.SetOut(writer);

            //RefreshAll(excelFile, pwdFile, removeDataLink);

            //var excelTemp = string.Format(TempPath, Path.GetFileName(excelFile));

            //File.Copy(excelTemp, pathToSaveRefreshedFile, true);
            //Console.WriteLine("File copied to Folder");

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static void RefreshAll(string excelFile, string pwdFile, bool removeDataLink)
        {
            try
            {
                Console.WriteLine("Refreshing file {0}", excelFile);

                if (!File.Exists(excelFile))
                {
                    Console.WriteLine("File was not found");
                    return;
                }

                var excelTemp = string.Format(TempPath, Path.GetFileName(excelFile));

                Console.WriteLine("Copying file local");
                File.Copy(excelFile, excelTemp, true);

                ExcelHelper.RefreshAll(excelTemp, pwdFile, removeDataLink);
                Console.WriteLine("Pivots are refreshed");
            }
            catch (Exception e)
            {
                Console.WriteLine("#ERROR Pivots are NOT refreshed. {0}", e.Message);
            }
        }
    }
}
